package com.wzc.system.controller;

import com.wzc.basic.annotation.PreAuthorize;
import com.wzc.basic.query.BaseQuery;
import com.wzc.basic.util.AjaxResult;
import com.wzc.basic.util.PageList;
import com.wzc.system.domain.Role;
import com.wzc.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色管理接口类",description = "角色管理相关CRUD")
public class RoleController {


    @Autowired
    private IRoleService roleService;


    /**
     * 查询所有
     * @GetMapping
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
//    @PreAuthorize(name = "角色管理查询所有", sn = "role:list")
    public List<Role> loadAll(){
        return roleService.loadAll();
    }


    /**
     * 查询单个
     * @GetMapping("/{id}") 后端接收前端传输的数据（动态传输）
     * @PathVariable("id") 从路径上去获取id变量（后端取数据）再赋值给 Long id
     * @param id
     * @return
     */
    @GetMapping("/{id}")  // 接收参数 @PathVariable获取参数
    @ApiOperation(value = "通过id查询单个数据")
//    @PreAuthorize(name = "角色管理通过id查询单个数据", sn = "role:loadById")
    public Role loadById(@PathVariable("id") Long id){
        return roleService.loadById(id);
    }

    /**
     * @RequestBody  从请求体中获取数据赋值给roleQuery。
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
//    @PreAuthorize(name = "角色管理分页查询", sn = "role:queryPage")
    public PageList<Role> queryPage(@RequestBody BaseQuery query){
        try {
            return roleService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Role>();
        }
    }


    /**
     * 删除
     * @DeleteMapping("/{id}") 后端接收前端参数
     * @PathVariable("id")  从资源路径上获取参数id，在赋值给Long id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
//    @PreAuthorize(name = "角色管理删除操作", sn = "role:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            roleService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "角色管理批量删除操作", sn = "role:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            roleService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }

    }




    /**
     * 添加和修改
     * @PutMapping 请求体携带参数
     * @RequestBody  从请求体中获取参数
     * @param role
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
//    @PreAuthorize(name = "角色管理添加或修改操作", sn = "role:saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Role role){
        try {
            if (role.getId() != null){
                roleService.update(role);
            } else {
                roleService.save(role);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败!!!!!!!!!!!!!!");
        }
    }


}
